import Vue from 'vue';
import VueRouter, { RouteConfig, Route } from 'vue-router';
import Layout from '@/layout/index.vue';

Vue.use(VueRouter);

export const constantRoutes: RouteConfig[] = [
  {
    path: '/redirect',
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import(/* webpackChunkName: "redirect" */ '@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    children: [
      {
        name: 'index',
        path: 'index',
        component: () =>
        import(/* webpackChunkName: "index" */ '@/views/index.vue'),
        meta: {
          icon: 'hamburger',
          title: 'index',
          affix: true,
        },
      },
    ],
  }
];

export const asyncRoutes: RouteConfig[] = [
  {
    path: '/athority-center',
    component: Layout,
    redirect: '/athority-list',
    meta: {
      title: 'athorityCenter',
      icon: 'hamburger',
      roles: ['admin', 'editor'],
      alwaysShow: true,
      breadcrumb: false
    },
    children: [
      {
        path: 'athority-list',
        component: () =>
          import(/* webpackChunkName: "athority-list" */ '@/views/athority_center/athority_list.vue'),
        name: 'athorityList',
        meta: {
          title: 'athorityList',
          roles: ['admin'],
        },
      },
    ],
  },
];

const createRouter = () =>
  new VueRouter({
    mode: 'history',
    scrollBehavior: (to, from, savedPosition) => {
      if (savedPosition) {
        return savedPosition;
      } else {
        return { x: 0, y: 0 };
      }
    },
    // base: process.env.BASE_URL,
    routes: constantRoutes,
  });

const router = createRouter();

export function resetRouter() {
  const newRouter = createRouter();
  (router as any).matcher = (newRouter as any).matcher;
}

// router.beforeEach((to: Route, from: Route, next: any) => {
//   // ...
//   next();
// });

export default router;
